﻿using System;
using System.Threading;
namespace CSUtils {
    public class CDelayBg: CDelay0<CDelayBg> {
        const string _tag = "CSUtils.CDelayBg.线程";

        public static void Init() {
            _tag0 = "CDelay0.bg";
            CDelayBgUpdate.Init();
        }

        public static void AppEnd() {
            CDelayBgUpdate.AppEnd();
        }
    }

    internal class CDelayBgUpdate {
        const string _tag = "CSUtils.CDelayBg.线程";
        static Thread thread;

        internal static void Init() {
            if(thread != null) {
                return;
            }
            #if CDelayMainTest
            return;
            #endif
            CLog.i(_tag, $"Init()...appIsEnd={appIsEnd}....ThreadId={Thread.CurrentThread.ManagedThreadId}");
            thread = new Thread(delegate() {
                while(appIsEnd == false) {
                    //CLog.i(_tag, $"while()...........{CTime.currTimeStr}.....{CTime.deltaTime2}.");
                    Thread.Sleep(14);
                    try {
                        CDelayBg.IUpdate(_tag);
                    } catch(Exception ex) {
                        CLog.Ex(_tag, "错误", ex);
                    }
                }
            });
            thread.IsBackground = true;
            thread.Name = _tag;
            thread.Start();
            //CDelayBg2.SetThreadId(thread.ManagedThreadId);
        }

        static bool appIsEnd;

        public static void AppEnd() {
            appIsEnd = true;
        }
    }
}
